package com.study.talk.app.register.fragment;

import static androidx.navigation.fragment.FragmentKt.findNavController;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.study.talk.R;
import com.study.talk.app.base.BaseFragment;
import com.study.talk.app.base.EventBusBaseFragment;
import com.study.talk.databinding.FragmentCodeBinding;
import com.study.talk.netty.NettyClient;
import com.study.talk.netty.message.request.SmsSendRequest;
import com.study.talk.netty.message.response.SmsSendResponse;
import com.study.talk.util.ToastUtil;
import com.study.talk.util.alert.AlertUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class CodeFragment extends EventBusBaseFragment {


    private FragmentCodeBinding fragmentCodeBinding;
    private CodeFragmentArgs codeFragmentArgs;
    private String CODE;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        fragmentCodeBinding = FragmentCodeBinding.inflate(inflater,container,false);
        codeFragmentArgs = CodeFragmentArgs.fromBundle(getArguments());
        CODE = codeFragmentArgs.getCode();
        return fragmentCodeBinding.getRoot();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        fragmentCodeBinding.codeNotice.setText(codeFragmentArgs.getPhoneNumber());
        fragmentCodeBinding.codeFragmentEditView.setInputCompleteListener(code -> {
            if (code.equals(CODE)){
                findNavController(this).navigate(R.id.action_codeFragment_to_settingPasswordFragment, codeFragmentArgs.toBundle());
            }else {
                AlertUtil.show(getActivity(),"错误提醒","验证码错误");
            }
        });

        fragmentCodeBinding.reSendCodeBtn.setOnClickListener(v -> {
            NettyClient.sendMessage(new SmsSendRequest(codeFragmentArgs.getUsername(),codeFragmentArgs.getPhoneNumber()));
            fragmentCodeBinding.reSendCodeBtn.setClickable(false);
            new CountDownTimer(60000, 1000) {
                @SuppressLint("SetTextI18n")
                @Override
                public void onTick(long millisUntilFinished) {
                    fragmentCodeBinding.reSendCodeBtn.setText( (millisUntilFinished / 1000) + "秒后重新发送" );
                }

                @Override
                public void onFinish() {
                    fragmentCodeBinding.reSendCodeBtn.setText("重新发送验证码");
                    fragmentCodeBinding.reSendCodeBtn.setClickable(true);
                }
            }.start();
        });


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSmsMessage(SmsSendResponse message){
        if (message.getStatus().equals("OK")){
            CODE = message.getCode();
        }else {
            ToastUtil.showToast(getContext(),message.getMessage());
        }

    }
}